/**
 * Created by zhangyf on 2016/4/26.
 */
//--map--
sky.define(['sky/dom/kite'],function(require){
    var kite = require('sky/dom/kite');
    var U = require('util');

    // 销毁弹窗
    function destroy(){
        this.destroy();
    }

    // 默认
    var option = {
        title_text:'提示信息',
        con_text:'信息内容',
        close:'<div class="modals_close" sky-kite="close"></div>'
    }

    // 更改默认配置
    function config(opt){
        return U.extra(opt, option);
    }

    // 获取当前 option
    function getOption(opt1, opt2){
        var opt = U.extra(option, opt2 || {}, {});
        if(typeof opt1 == 'string'){
            opt.con_text = opt1
        }
        else{
            U.extra(opt1, opt);
        }
        return opt;
    }

    // 替换特殊表示的字符
    function replaceText(str, opt){
        return str.replace(/\{#(\w+)\}/g, function($0,$1){
             return opt[$1] || '';
        })
    }

    // ============ 自定义 弹窗  =====================
    var string_prompt = '<div class="sky-modals {#opt_class}"><div class="modals_cc">{#close}<div class="modals_t" sky-kite="title">{#title_text}</div><div class="modals_c" sky-kite="con">{#con_text}</div>{#buttons}</div></div>';
    // alert 弹窗
    var string_alert = string_prompt.replace(/\{#buttons\}/g,'<div class="modals_c_b1" sky-kite="btns"><div class="modals_btn" sky-kite="btn_ok"><span>{#btn_ok_text}</span></div></div>');
    // confiorm 弹窗
    var string_confiorm = string_prompt.replace(/\{#buttons\}/g,'<div class="modals_c_b2" sky-kite="btns"><div class="modals_btn" sky-kite="btn_cancel"><span>{#btn_cancel_text}</span></div><div class="modals_btn" sky-kite="btn_ok"><span>{#btn_ok_text}</span></div></div>');

    var Panel = U.extend(kite.MaskClass, {
        // 通用弹窗
        prompt: function(str,buttons){
            var opt = getOption(str,{buttons:buttons || ''});
            return this.create(replaceText(string_prompt,opt)).drag('title').bind('close',destroy).offset(.5,.5);
        },
        // alert
        alert: function(str,fn){
            var opt = getOption(str,{btn_ok_text:'确认'});
            return this.create(replaceText(string_alert,opt),'btn_ok').drag('title').bind('close',destroy).offset(.5,.5).bind('btn_ok_vclick',fn || destroy);
        },
        // confirm
        confirm: function(str,fn,fn1){
            var opt = getOption(str,{btn_ok_text:'确认',btn_cancel_text:'取消'});
            return this.create(replaceText(string_confiorm,opt),'btn_ok').drag('title').bind('close',destroy).offset(.5,.5).bind('btn_ok_vclick',fn || destroy).bind('btn_cancel_vclick',fn1 || destroy);
        }
    },function(){
        arguments.callee._inits_[0].apply(this,arguments);
    })

    var  modals = new Panel('#000000',.5,1000000);
    modals.config = config;
    modals.kite = kite;
    // 创建新的
    modals.createMask = function(bg,opacity,zindex){
        return new Panel(bg,opacity,zindex);
    }

    modals.BaseClass = Panel;
    return modals;
});